﻿using System.Xml.Linq;
using System.Xml.Serialization;

namespace RationalXml
{
    /// <summary>
    /// This is an example of using XmlBase as a starting point for XML serialization
    /// You must specify a XmlRoot attribute for classes used by XmlSerialzer as the root of your XML document
    /// </summary>
    [XmlRoot(Namespace = "http://billthorp.wordpress.com/ExampleXml.xsd")]
	public class ExampleXml : XmlBase
	{
        /// <summary>
        /// namespaces aren't required... but they should match anything defined by the [XmlRoot] attribute
        /// </summary>
        public override XNamespace Ns
        {
            get { return "http://billthorp.wordpress.com/ExampleXml.xsd"; }
        }

        /// <summary>
        /// Implement your class as required
        /// </summary>
	    public string ItemOne { get; set; }
		public string ItemTwo { get; set; }

        /// <summary>
        /// Implement your custom serialization logic using XElements
        /// </summary>
		public override void ReadXml(XElement xElement)
		{
			XNamespace ns = xElement.GetDefaultNamespace();
            ItemOne = (string)xElement.Element(ns + "UselessXmlContainer").Element(ns + "ItemOne");
            ItemTwo = (string)xElement.Element(ns + "ItemTwo");
		}

        /// <summary>
        /// Implement your custom deserialization logic using XElements
        /// </summary>
		public override XElement WriteXml()
		{
            return new XElement(Ns + "ExampleXml",
                 new XElement(Ns + "UselessXmlContainer", new XElement(Ns + "ItemOne", ItemOne)),
                 new XElement(Ns + "ItemTwo", ItemTwo)
            );
		}
	}
}
